<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix" style="text-align: left;">
      <span>{{role == 3 ? "我的成绩" :"成绩信息"}}</span>
    </div>   
    <div style="display: flex;align-items: center;margin-bottom:10px" v-if="role != 3">
      <el-input
        style="width: 250px;margin-right: 10px;"
        placeholder="请输入课程名称"
        v-model="name"
        clearable
      ></el-input>
      <el-button type="primary" @click="query">查询</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="studentCourse.course.courseNo" label="课程号"></el-table-column>
      <el-table-column prop="studentCourse.course.name" label="课程名"></el-table-column>
      <el-table-column prop="studentCourse.course.teacher.realName" label="授课教师"></el-table-column>
      <el-table-column prop="studentCourse.student.name" label="学生"></el-table-column>
      <el-table-column prop="studentCourse.course.period" label="学时"></el-table-column>
      <el-table-column prop="studentCourse.course.score" label="学分"></el-table-column>

      <el-table-column prop="score" label="成绩">
      </el-table-column>
      <el-table-column label="操作" width="120" v-if="role != 3">
        <template scope="scope">
          <el-button
            type="text"
            size="small"
            @click="dafen(scope.row.id)"
          >重新打分</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:left;margin-top:25px"
      background
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="getPage"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="show" width="500px">
      <el-form label-width="100px" size="small">
        <el-form-item label="分数" :required="true">
          <el-input style="width: 280px;" v-model="score.score"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>
  
<script>
import { request } from "../request.js";
export default {
  name: "xuanke",
  data() {
    return {
      name: "",
      role: localStorage.getItem("role"),
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      totalCount: 0,

      title: "分数编辑",
      show: false,
      update: false,
      score: {
        id: "",
        score: ""
      },
      results: {
        0: "未考核",
        1: "已通过",
        2: "未通过",
        3: "已退选"
      }
    };
  },

  created() {
    this.getData();
  },
  methods: {
    /**
     * 查询
     */
    query() {
      this.pageNo = 1;
      this.getData();
    },

    /**
     * 获取分页数据getData() 
     */
    getData() {
      let param = {
        name: this.name,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      };

      request
        .request("/score/getScoreList", "POST", param, this)
        .then(data => {
          data = data.data;
          this.tableData = data.list;
          this.pageNo = data.page.currentPage;
          this.totalPage = data.page.totalCount;
        });
    },

    /**
     * 分页器翻页
     */
    getPage(page) {
      this.pageNo = page;
      this.getData();
    },

    /**
     * 提交打分
     */
    submit() {
      if (!this.score.score) {
        this.$message.error("请输入课程学分");
        return;
      }
      request.request("score/editScore", "POST", this.score, this).then(() => {
        this.score.id = "";
        this.score.score = "";
        this.$message.success("成绩成功");
        this.show = false;
        this.query();
      });
    },

    /**
     * 打分
     */
    dafen(id) {
      this.score.id = id;
      this.show = true;
      this.title = "编辑分数";
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
